﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Scheduling.Models.Interfaces;

namespace Scheduling.Models.Repos.InMemory
{
    public class InMemoryCourseInstanceLoader : ICourseInstanceLoader
    {
        private List<CourseInstance> CourseInstanceList;

        public InMemoryCourseInstanceLoader()
        {
            CourseInstanceList = new List<CourseInstance>();
            CourseInstance courseInstance1 = new CourseInstance();
            CourseInstance courseInstance2 = new CourseInstance();
            courseInstance1.Course = new Course() { ID = "DVGA24", Name = "Introduction to computer science", Points = 7.5d };
            courseInstance2.Course = new Course() { ID = "DVGC01", Name = "Programming language", Points = 7.5d };
            courseInstance1.ID = 75000;
            courseInstance2.ID = 76000;
            courseInstance1.OnCampus = true;
            courseInstance2.OnCampus = true;
            courseInstance1.Period = 2;
            courseInstance2.Period = 4;
            courseInstance1.ProgressionSpeed = 50;
            courseInstance2.ProgressionSpeed = 50;
            courseInstance1.StartWeek = 14;
            courseInstance1.EndWeek = 23;
            courseInstance2.StartWeek = 35;
            courseInstance2.EndWeek = 44;
            CourseInstanceList.Add(courseInstance1);
            CourseInstanceList.Add(courseInstance2);
        }

        public List<CourseInstance> GetCourseInstances()
        {
            return CourseInstanceList;
        }

        public bool CourseInstanceExists(int CourseInstanceID)
        {
            return CourseInstanceList.Count(c => c.ID == CourseInstanceID) > 0;
        }

        public CourseInstance GetCourseInstance(int CourseInstanceID)
        {
            if (CourseInstanceList.Count(c => c.ID == CourseInstanceID) > 0)
                return CourseInstanceList.Where(c => c.ID == CourseInstanceID).First();
            else
                return null;
        }


        public List<CourseInstance> GetCourseInstancesByCourseID(string SearchCourseID)
        {
            List<CourseInstance> fetchList = new List<CourseInstance>();
            foreach (CourseInstance c in CourseInstanceList)
                if (c.Course.ID == SearchCourseID)
                    fetchList.Add(c);
            return fetchList;
        }


        public List<CourseInstance> GetCourseInstancesByQuery(string Query, int MinYear = 1970, int MaxYear = 2100, int Limit = 10)
        {
            return CourseInstanceList.Where(c => 
                    (c.Course.ID.Contains(Query) || c.Course.Name.Contains(Query)) && 
                    c.Year < MaxYear && 
                    c.Year > MinYear)
                .Take(Limit).ToList();
        }

    }
}